import requests
from bs4 import BeautifulSoup


# proxy = {
#     'https': '121.61.3.43:9999'
# }
# url = 'https://www.ip.cn/'
# page = requests.get(url=url, proxies=proxy)
# print(page.text)


def request_page(url):
    page = requests.get(url=url,)
    page.encoding = page.apparent_encoding
    return page.text


def parse_html(page, flag):
    soup = BeautifulSoup(page, 'lxml')
    ip = soup.select('td[data-title="IP"]')
    port = soup.select('td[data-title="PORT"]')
    for k, v in zip(ip, port):
        res = {}
        res['ip'] = k.text
        res['port'] = v.text
        flag.append(res)
        print(k.text + ":" + v.text)


import time

url = 'https://www.kuaidaili.com/free/inha/{0}/'
if __name__ == '__main__':
    flag = []
    for i in range(41, 45):
        parse_html(request_page(url=url.format(i)), flag)
        time.sleep(1)

    import json

    # json.dump(flag, open('ip.json', 'a', encoding='utf8'))
